namespace Microsoft.VisualStudio.EnterpriseTools.OperationDesigner
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    internal static class SummaryHelper
    {
        private static bool FindWhiteSpaceIndices(string summary, out int leadingIndex, out int trailingIndex, bool allowMultiLine)
        {
            leadingIndex = 0;
            while ((leadingIndex < summary.Length) && ((summary[leadingIndex] == '\r') || (summary[leadingIndex] == '\n')))
            {
                leadingIndex++;
            }
            trailingIndex = summary.Length - 1;
            if (allowMultiLine)
            {
                while ((trailingIndex > 0) && ((summary[trailingIndex] == '\r') || (summary[trailingIndex] == '\n')))
                {
                    trailingIndex--;
                }
            }
            else
            {
                trailingIndex = summary.IndexOf(Environment.NewLine, leadingIndex);
                if (trailingIndex == -1)
                {
                    trailingIndex = summary.Length - 1;
                }
            }
            if (leadingIndex >= trailingIndex)
            {
                return false;
            }
            if (leadingIndex == 0)
            {
                return (trailingIndex != (summary.Length - 1));
            }
            return true;
        }

        internal static string RestoreValue(string currentValue, string newValue, bool allowMultiLine)
        {
            int leadingIndex;
            int trailingIndex;
            if (FindWhiteSpaceIndices(currentValue, out leadingIndex, out trailingIndex, allowMultiLine))
            {
                string text = currentValue.Substring(0, leadingIndex);
                string text2 = currentValue.Substring(trailingIndex + 1);
                StringBuilder builder = new StringBuilder(text, (text.Length + currentValue.Length) + text2.Length);
                builder.Append(newValue);
                builder.Append(text2);
                return builder.ToString();
            }
            return newValue;
        }

        internal static string StripValue(string currentValue, bool allowMultiLine)
        {
            int leadingIndex;
            int trailingIndex;
            if (FindWhiteSpaceIndices(currentValue, out leadingIndex, out trailingIndex, allowMultiLine))
            {
                currentValue = currentValue.Substring(leadingIndex, (trailingIndex - leadingIndex) + 1);
            }
            return currentValue;
        }

        internal static string TextToWSDLDescription(string description)
        {
            return description.Replace(Environment.NewLine, string.Empty);
        }

        internal static string WSDLDescriptionToText(string description)
        {
            return description;
        }
    }
}

